package com.snow.useviewdemo.function;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.snow.common.base.BaseBasesActivity;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.function.surface.LivePlayView;
import com.snow.useviewdemo.function.surface.LivePlayerManager;
import com.snow.useviewdemo.html.TBSWebView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-11-04
 * desc   :
 */
public class TestSurfaceViewActivity extends BaseBasesActivity {
    @BindView(R.id.btn_01)
    Button btn01;
    @BindView(R.id.btn_02)
    Button btn02;
    @BindView(R.id.teacher_play_view)
    LivePlayView teacherPlayView;
    @BindView(R.id.teacher_rl)
    RelativeLayout teacherRl;
    @BindView(R.id.tBSWebView)
    TBSWebView tBSWebView;
    private LivePlayerManager teacherLPM;
    public static final String TEST_VIDEO = "https://cdn.changguwen.com/cms/media/20181024/0dbfb433-d976-4c54-9026-272ca2967f46-1540367091203-sd.mp4";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_test_surfaceview;
    }

    @Override
    protected void initView() {
        //第二个视频
        teacherLPM = new LivePlayerManager(this);
        teacherPlayView.setLivePlayer(teacherLPM.getaliyunVodPlayer());
        tBSWebView.loadUrl("https://m-test.changguwen.com/live-mvp?token=R9D82NqwmJ7jdAVeW9DzeyrgW6BXbLQp");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_01, R.id.btn_02})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_01:
                teacherView(TEST_VIDEO);
                break;
            case R.id.btn_02:
                teacherView(TEST_VIDEO);
                showStudentViewAnimation(teacherRl, teacherPlayView);
                break;
        }
    }

    //老师播放View
    private void teacherView(String url) {
        teacherPlayView.setVisibility(View.VISIBLE);
        teacherRl.setVisibility(View.VISIBLE);
//            teacherLPM.setPlaySource(AppConstant.TEST_VIDEO);
        teacherPlayView.setLivePlayer(teacherLPM.getaliyunVodPlayer());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        teacherLPM.stop();
    }

    /**
     * 学生View出现动效
     */
    private void showStudentViewAnimation(View view, LivePlayView playView) {
        ObjectAnimator rotateX = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f);
        ObjectAnimator rotateY = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
//        ObjectAnimator surfaceViewX = ObjectAnimator.ofFloat(mBinding.studentPlayView, "scaleX", 0f, 1f);
//        ObjectAnimator surfaceViewY = ObjectAnimator.ofFloat(mBinding.studentPlayView, "scaleY", 0f, 1f);
//        ObjectAnimator surfaceViewAlpha = ObjectAnimator.ofFloat(mBinding.studentPlayView, "alpha", 0f, 1f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(rotateX).with(rotateY).with(alpha);
//                .with(surfaceViewX).with(surfaceViewY).with(surfaceViewAlpha);
        animSet.setDuration(2000);
        animSet.start();
        animSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
//                playView.setVisibility(View.VISIBLE);
                teacherLPM.setPlaySource(TEST_VIDEO);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }
}
